package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.commom.exception.YyghException;
import com.atguigu.yygh.commom.result.Result;
import com.atguigu.yygh.commom.result.ResultCodeEnum;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author mengWu
 * @Date 2021年05月17日 15:06
 * 所有对外的接口
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    //排班接口 -- 删除
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号和排班编号
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        //签名的校验
        //1、获取医院传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2、根据传递过来的医院编码，查询数据库，查询签名 -- 上面已经获取到签名了
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3、把数据库查询出来的结果进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //进行判断 -- 签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用对应的方法
        scheduleService.remove(hoscode,hosScheduleId);

        return Result.ok();



    }

    //排班接口 -- 查询
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //科室编号
        String depcode = (String) paramMap.get("depcode");


        //当前页 -- 如果传过来的当前页数据为空，设为默认值1，如果不为空，则使用传过来的值
        int page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String) paramMap.get("page"));
        //每页记录数
        int limit = StringUtils.isEmpty(paramMap.get("limit"))?10:Integer.parseInt((String) paramMap.get("limit"));

        //签名校验
        //1、获取医院传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2、根据传递过来的医院编码，查询数据库，查询签名 -- 上面已经获取到签名了
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3、把数据库查询出来的结果进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //进行判断 -- 签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();

        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        //调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);

        return Result.ok(pageModel);




    }

    //排班接口 -- 上传排班的接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String) paramMap.get("hoscode");

        //签名的校验
        //1、获取医院传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2、根据传递过来的医院编码，查询数据库，查询签名 -- 上面已经获取到签名了
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3、把数据库查询出来的结果进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //进行判断 -- 签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service的方法
        scheduleService.save(paramMap);

        return Result.ok();



    }

    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        //签名的校验
        //1、获取医院传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2、根据传递过来的医院编码，查询数据库，查询签名 -- 上面已经获取到签名了
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3、把数据库查询出来的结果进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //进行判断 -- 签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service中的方法 -- 传入两个参数，一个是医院编号，一个是科室编号
        departmentService.remove(hoscode,depcode);

        return Result.ok();
    }

    //查询科室的接口
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //当前页 -- 如果传过来的当前页数据为空，设为默认值1，如果不为空，则使用传过来的值
        int page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String) paramMap.get("page"));
        //每页记录数
        int limit = StringUtils.isEmpty(paramMap.get("limit"))?10:Integer.parseInt((String) paramMap.get("limit"));

        //进行签名的校验
        //签名校验
        //1、获取医院传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2、根据传递过来的医院编码，查询数据库，查询签名 -- 上面已经获取到签名了
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3、把数据库查询出来的结果进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //进行判断 -- 签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //获取查询条件
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();

        departmentQueryVo.setHoscode(hoscode);

        //调用service方法
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);

        return Result.ok(pageModel);
    }
    //上传科室的接口
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //验证数字签名是否匹配

        //获取传递过来的医院编号
        String hoscode = (String) paramMap.get("hoscode");

        //签名校验
        //1、获取医院传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2、根据传递过来的医院编码，查询数据库，查询签名 -- 上面已经获取到签名了
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3、把数据库查询出来的结果进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //进行判断 -- 签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service中的方法
        departmentService.save(paramMap);

        return Result.ok();

    }

    //查询接口
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取传递过来的医院编号
        String hoscode = (String) paramMap.get("hoscode");

        //签名校验
        //1、获取医院传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2、根据传递过来的医院编码，查询数据库，查询签名 -- 上面已经获取到签名了
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3、把数据库查询出来的结果进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //进行判断 -- 签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //签名一致，进行下一步,调用service的方法，根据医院的编号进行查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);

        return Result.ok(hospital);


    }

    //上传医院的接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1、获取医院传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2、根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3、把数据库查询出来的结果进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //进行判断 -- 签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //传输过程中，“+”转换成了“ ”，因此我们需要转换回来
        //1、将logoData数据强转成字符串
        String logoData = (String) paramMap.get("logoData");
        //2、将空格转换成加号
        logoData = logoData.replaceAll(" ", "+");

        paramMap.put("logoData", logoData);

        //调用service中的方法，将数据加入数据库
        hospitalService.save(paramMap);
        return Result.ok();
    }
}
